@extends('layouts.base')

@section('title', '会员兑换')

@push('styles')
<style>
    @keyframes float {
        0%, 100% { transform: translateY(0px) rotate(0deg); }
        50% { transform: translateY(-20px) rotate(180deg); }
    }

    @keyframes bounce {
        0%, 20%, 50%, 80%, 100% { transform: translateY(0); }
        40% { transform: translateY(-10px); }
        60% { transform: translateY(-5px); }
    }

    .redeem-container {
        padding: 30px 20px;
    }

    .redeem-form {
        background: white;
        border-radius: 20px;
        padding: 30px;
        box-shadow: 0 10px 40px rgba(0,0,0,0.1);
        margin-bottom: 30px;
    }

    .form-title {
        font-size: 20px;
        font-weight: 700;
        color: #2d3748;
        margin-bottom: 20px;
        text-align: center;
    }

    .code-input-group {
        position: relative;
        margin-bottom: 25px;
    }

    .code-input {
        width: 100%;
        padding: 15px 20px;
        border: 2px solid #e2e8f0;
        border-radius: 15px;
        font-size: 16px;
        font-weight: 600;
        text-align: center;
        letter-spacing: 2px;
        text-transform: uppercase;
        transition: all 0.3s ease;
        background: #f8f9fa;
    }

    .code-input:focus {
        outline: none;
        border-color: #667eea;
        background: white;
        box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }

    .code-input::placeholder {
        color: #a0aec0;
        font-weight: 400;
        letter-spacing: 1px;
    }

    .redeem-btn {
        width: 100%;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border: none;
        color: white;
        padding: 15px;
        border-radius: 15px;
        font-size: 16px;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        position: relative;
        overflow: hidden;
    }

    .redeem-btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 10px 30px rgba(102, 126, 234, 0.3);
    }

    .redeem-btn:disabled {
        background: #e2e8f0;
        color: #a0aec0;
        cursor: not-allowed;
        transform: none;
        box-shadow: none;
    }

    .redeem-btn .loading {
        display: none;
    }

    .redeem-btn.loading .loading {
        display: inline-block;
        animation: spin 1s linear infinite;
    }

    .redeem-btn.loading .text {
        display: none;
    }

    @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
    }

    .tips-section {
        background: white;
        border-radius: 16px;
        padding: 20px;
        box-shadow: 0 4px 20px rgba(0,0,0,0.08);
        margin-bottom: 20px;
    }

    .tips-title {
        font-size: 16px;
        font-weight: 600;
        color: #2d3748;
        margin-bottom: 15px;
        display: flex;
        align-items: center;
    }

    .tips-title::before {
        content: '💡';
        margin-right: 8px;
    }

    .tips-list {
        list-style: none;
        padding: 0;
        margin: 0;
    }

    .tips-list li {
        color: #718096;
        font-size: 14px;
        line-height: 1.6;
        margin-bottom: 8px;
        padding-left: 20px;
        position: relative;
    }

    .tips-list li::before {
        content: '•';
        color: #667eea;
        font-weight: bold;
        position: absolute;
        left: 0;
    }

    .history-link {
        background: white;
        border-radius: 16px;
        padding: 20px;
        box-shadow: 0 4px 20px rgba(0,0,0,0.08);
        text-decoration: none;
        color: inherit;
        display: block;
        transition: all 0.3s ease;
    }

    .history-link:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 30px rgba(0,0,0,0.12);
        color: inherit;
        text-decoration: none;
    }

    .history-content {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    .history-info {
        display: flex;
        align-items: center;
    }

    .history-icon {
        width: 48px;
        height: 48px;
        border-radius: 12px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 24px;
        margin-right: 15px;
        color: white;
    }

    .history-text {
        flex: 1;
    }

    .history-title {
        font-size: 16px;
        font-weight: 600;
        color: #2d3748;
        margin: 0 0 4px 0;
    }

    .history-desc {
        font-size: 14px;
        color: #718096;
        margin: 0;
    }

    .history-arrow {
        color: #a0aec0;
        font-size: 18px;
    }

    .success-animation {
        display: none;
        text-align: center;
        padding: 40px 20px;
    }

    .success-icon {
        font-size: 64px;
        margin-bottom: 20px;
        animation: successBounce 0.6s ease-out;
    }

    @keyframes successBounce {
        0% { transform: scale(0); }
        50% { transform: scale(1.2); }
        100% { transform: scale(1); }
    }

    .success-title {
        font-size: 24px;
        font-weight: 700;
        color: #48bb78;
        margin-bottom: 10px;
    }

    .success-desc {
        color: #718096;
        font-size: 16px;
    }
</style>
@endpush

@section('content')
<div class="page-header">
    <button class="back-btn" onclick="window.location.href = '{{ route('membership.benefits') }}'"><</button>
    <div class="page-icon">🎫</div>
    <div class="page-title">会员兑换</div>
    <div class="page-subtitle">输入兑换码，立即升级会员</div>
</div>

<div class="redeem-container">
    <div class="redeem-form" id="redeemForm">
        <div class="form-title">输入兑换码</div>
        <div class="code-input-group">
            <input type="text" class="code-input" id="redeemCode" placeholder="请输入兑换码" maxlength="16">
        </div>
        <button class="redeem-btn" id="redeemBtn" onclick="processRedeem()">
            <span class="loading">⟳</span>
            <span class="text">立即兑换</span>
        </button>
    </div>

    <div class="success-animation" id="successAnimation">
        <div class="success-icon">🎉</div>
        <div class="success-title">兑换成功！</div>
        <div class="success-desc">恭喜您成功升级为会员，享受专属特权</div>
    </div>

    <div class="tips-section">
        <div class="tips-title">兑换说明</div>
        <ul class="tips-list">
            <li>兑换码由16位字母和数字组成，不区分大小写</li>
            <li>每个兑换码只能使用一次，兑换后立即生效</li>
            <li>兑换码有效期请以实际获得的兑换码为准</li>
            <li>如遇兑换问题，请联系客服获得帮助</li>
        </ul>
    </div>

    <a href="{{ route('membership.history') }}" class="history-link">
        <div class="history-content">
            <div class="history-info">
                <div class="history-icon">📋</div>
                <div class="history-text">
                    <div class="history-title">兑换记录</div>
                    <div class="history-desc">查看历史兑换记录</div>
                </div>
            </div>
            <div class="history-arrow">›</div>
        </div>
    </a>
</div>
@endsection

@push('scripts')
<script src="{{ asset('js/jquery.min.js') }}"></script>
<script src="{{ asset('js/bootstrap.bundle.min.js') }}"></script>
<script>
    // 兑换码输入格式化
    $('#redeemCode').on('input', function() {
        let value = $(this).val().toUpperCase().replace(/[^A-Z0-9]/g, '');
        if (value.length > 16) {
            value = value.substring(0, 16);
        }
        $(this).val(value);

        // 启用/禁用兑换按钮
        if (value.length >= 8) {
            $('#redeemBtn').prop('disabled', false);
        } else {
            $('#redeemBtn').prop('disabled', true);
        }
    });

    // 处理兑换
    function processRedeem() {
        const code = $('#redeemCode').val().trim();
        const btn = $('#redeemBtn');

        if (code.length < 8) {
            showToast('请输入有效的兑换码', 'error');
            return;
        }

        const originalText = btn.html();

        // 显示加载状态
        btn.prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> 兑换中...');

        // 发送兑换请求
        $.ajax({
            url: '{{ route("membership.redeem") }}',
            method: 'POST',
            data: {
                code: code,
                _token: $('meta[name="csrf-token"]').attr('content')
            },
            success: function(response) {
                if (response.success) {
                    $('#code').val(''); // 清空输入框
                    showToast(response.message);

                    $('#redeemForm').fadeOut(300, function() {
                        $('#successAnimation').fadeIn(300);
                    });

                    // 3秒后跳转到会员权益页面
                    setTimeout(() => {
                        location.href = '{{ route("membership.benefits") }}';
                    }, 3000);
                } else {
                    showToast(response.message || '兑换失败，请重试', 'error');
                }
            },
            error: function(xhr) {
                let message = '兑换失败，请重试';
                if (xhr.responseJSON && xhr.responseJSON.message) {
                    message = xhr.responseJSON.message;
                }
                showError(message);
            },
            complete: function() {
                // 恢复按钮状态
                btn.prop('disabled', false).html(originalText);
            }
        });
    }

    // 页面加载动画
    $(document).ready(function() {
        $('.redeem-form, .tips-section, .history-link').each(function(index) {
            $(this).css({
                'opacity': '0',
                'transform': 'translateY(20px)'
            }).delay(index * 100).animate({
                'opacity': '1'
            }, 500).css('transform', 'translateY(0)');
        });
    });
</script>
@endpush
